﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using vITs.Site.Models.DataModels;
using vITs.Site.Models;

namespace vITs.Site.Controllers
{
    public class ReportController : Controller
    {
        public ActionResult Assignment(Int32 AssignmentId, Boolean Print = false)
        {
            var assignment = (from Assignment a in new VitsDB().Assignments where a.AssignmentId == AssignmentId select a).SingleOrDefault();
            if (assignment == null)
            {
                return this.RedirectToAction("Index", "Dashboard");
            }

            if (assignment.Consultant.EmailAddress != this.User.Identity.Name && assignment.Handler.EmailAddress != this.User.Identity.Name && !this.User.IsInRole("Administratör"))
            {
                return this.RedirectToAction("Index", "Dashboard");
            }


            if (this.User.IsInRole("Administratör"))
            {
                if (assignment.GetStatus() == AssignmentStatus.AssignmentDraft)
                {
                    return this.RedirectToAction("Index", "Dashboard");
                }
            }

            this.ViewBag.Print = Print;

            if (Print)
            {
                return this.PartialView(assignment);
            }

            return this.View(assignment);
        }

        public ActionResult AssignmentListExcel(DateTime StartDate, DateTime EndDate)
        {
            VitsDB db = new VitsDB();
            if (StartDate == null || EndDate == null)
                return this.RedirectToAction("Index", "Dashboard");
            else
            {
                List<Assignment> assignments = db.Assignments.Where(a => a.StartDate >= StartDate && a.EndDate <= EndDate).ToList();
                ViewBag.Content = Utilities.CreateExcelBase(assignments);
                ViewBag.Filename = "ReportList" + DateTime.Today.ToString() + ".xls";
            }
            return View();
        }

        public ActionResult AssignmentExcel(int AssignmentID)
        {
            VitsDB db = new VitsDB();
            var assignment = db.Assignments.SingleOrDefault(a => a.AssignmentId == AssignmentID);
            return View(assignment);
        }
    }
}
